package com.myrobot.ui;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.myrobot.util.ImageUtil;
import com.myrobot.util.RobotUtil;

public class PointFrame extends JFrame {
	public static final String ACT_COMMAND_CAPTURE = "act_command_capture";
	public static final String ACT_COMMAND_OK = "act_command_ok";
	private JButton mCaptureBtn = null;
	private JButton mOkBtn = null;
	private ImagePanel mImagePanel = null;
	private String mCurrentAction = null;
	private Map<String, Point> mPoints = new HashMap<String, Point>();
	private OnActionPointListener mActionPointListener = null;
	public PointFrame(String[] actions) {
		super();
		JPanel panel = null;
//		setBounds(100, 100, 800, 600);
		mCaptureBtn = new JButton("截取");
		mCaptureBtn.setActionCommand("act_command_capture");
		mCaptureBtn.addActionListener(mActionListener);
		mOkBtn = new JButton("确定");
		mOkBtn.setActionCommand("act_command_ok");
		mOkBtn.addActionListener(mActionListener);
		panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, 0));
		panel.add(mCaptureBtn);
		panel.add(mOkBtn);
		for (String a : actions) {
			mPoints.put(a, null);
			JButton btn = new JButton(a);
			btn.setActionCommand(a);
			btn.addActionListener(mActionListener);
			panel.add(btn);
		}
		getContentPane().add(panel, "North");
		mImagePanel = new ImagePanel();
		getContentPane().add(mImagePanel, "Center");
		mImagePanel.addMouseListener(new java.awt.event.MouseListener() {
			public void mouseReleased(MouseEvent e) {
			}

			public void mousePressed(MouseEvent e) {
				if (mCurrentAction != null) {
					mPoints.put(mCurrentAction, e.getPoint());
					mImagePanel.setPoint(mCurrentAction, e.getPoint());
				}
			}

			public void mouseExited(MouseEvent e) {
			}

			public void mouseEntered(MouseEvent e) {
			}

			public void mouseClicked(MouseEvent e) {
			}
		});
		setExtendedState(JFrame.MAXIMIZED_BOTH);
		setVisible(true);
	}

	public void setActionPointListener(OnActionPointListener listener){
		mActionPointListener = listener;
	}
	
	private ActionListener mActionListener = new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			if (ACT_COMMAND_CAPTURE.equals(e.getActionCommand())) {
				setExtendedState(JFrame.ICONIFIED);
				BufferedImage image = RobotUtil.captureFullScreen();
				mImagePanel.setImage(image);
				setExtendedState(JFrame.MAXIMIZED_BOTH);
			} else if (ACT_COMMAND_OK.equals(e.getActionCommand())) {
				for(String a:mPoints.keySet()) {
					if(mPoints.get(a) == null) {
						Toolkit.getDefaultToolkit().beep();
						JOptionPane.showMessageDialog(null, "请选择Action\""+a+"\"坐标!", "ERROR_MESSAGE", 0);
						return;
					}
				}
				setVisible(false);
				if(mActionPointListener != null) {
					mActionPointListener.finish(mPoints);
				}
			} else {
				for (String a : mPoints.keySet()) {
					if (a.equals(e.getActionCommand())) {
						mCurrentAction = a;
					}
				}
			}
		}
	};

	class ImagePanel extends JPanel {
		private Image mImage;
		private Image mImageStar;
		private Map<String, Point> mPoints = new HashMap<String, Point>();

		public ImagePanel() {
			InputStream input = getClass().getResourceAsStream("/com/myrobot/res/star.jpg");
			mImageStar = ImageUtil.createImage(input);
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				input = null;
			}
		}

		public void setPoint(String action, Point p) {
			mPoints.put(action, p);
			repaint();
		}

		public void setImage(Image image) {
			mImage = image;
			repaint();
		}

		public void setImage(BufferedImage image) {
			mImage = image;
			repaint();
		}

		public Image getImage() {
			return mImage;
		}

		public void paint(Graphics g) {
			int w, h;
			if (mImage != null) {
				w = mImage.getWidth(null);
				h = mImage.getHeight(null);
				g.drawImage(mImage, 0, 0, w, h, null);
			}
			for (Point p : mPoints.values()) {
				w = mImageStar.getWidth(null);
				h = mImageStar.getHeight(null);
				g.drawImage(mImageStar, p.x - w / 2, p.y - h / 2, w, h, null);
			}
		}
	}
	public interface OnActionPointListener{
		public void cancel();
		public void finish(Map<String,Point> actionPoints);
	}
}

/*
 * Location: F:\runnable\yijieRobot.jar!\com\yijierobot\CaptureFrame.class Java
 * compiler version: 8 (52.0) JD-Core Version: 0.7.1
 */